package view;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Point;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;

import model.HistoryModel;
import model.PostMotemModel;
import model.complaint_TypeModel;
import model.userModel;

import com.toedter.calendar.JDateChooser;
import common.loadData;

import dao.FIRDAO;
import dao.HistoryDao;
import dao.PostMotemDao;
import dao.complaint_typeDao;
import dao.encrypt;
import dao.userdao;
import javax.swing.JRadioButton;

public class addPostmortem extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel Paneltitle = null;
	private JLabel lbPostmortem = null;
	private JPanel PanelInfo = null;
	private JLabel Labelname = null;
	private JLabel LabelAge = null;
	private JLabel LabelSex = null;
	private JLabel LabelAddress = null;
	private JLabel LabelImage = null;
	private JTextField txtname = null;
	private JTextField txtage = null;
	private JTextField txtaddress = null;
	private JTextField txtimage = null;
	private JButton Buttonchoose = null;
	private JPanel Panelimage = null;
	private JLabel Labelimage = null;
	private JPanel PanelDetail = null;
	private JLabel LabelDoctor = null;
	private JTextField txtDoctorname = null;
	private JLabel LabelChooseFir = null;
	private JComboBox ComboBoxFIR = null;
	private JLabel LabelPlace = null;
	private JTextField txtplace = null;
	private JLabel LabelDate = null;
	private JDateChooser txtDate;
	private JLabel LabelResult = null;
	private JTextArea txtResult = null;
	private JButton btnSave = null;
	private JButton btnClose = null;
	private JRadioButton RadioButtonMale = null;
	private JRadioButton RadioButtonFemale = null;
	private ButtonGroup group=null;  //  @jve:decl-index=0:

	public addPostmortem() {
		super();
		initialize();
		loadDataByID();
	}

	private void initialize() {
		this.setSize(580, 604);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/image/criminalTpye.png")));
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Add Postmortem");
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPaneltitle(), null);
			jContentPane.add(getPanelInfo(), null);
			jContentPane.add(getPanelDetail(), null);
			jContentPane.add(getBtnSave(), null);
			jContentPane.add(getBtnClose(), null);
		}
		return jContentPane;
	}

	private JPanel getPaneltitle() {
		if (Paneltitle == null) {
			lbPostmortem = new JLabel();
			lbPostmortem.setLocation(new Point(0, 0));
			lbPostmortem.setFont(new Font("Dialog", Font.BOLD, 18));
			lbPostmortem.setForeground(new Color(8, 66, 36));
			lbPostmortem.setText("Add Postmortem");
			lbPostmortem.setSize(new Dimension(152, 28));
			Paneltitle = new JPanel();
			Paneltitle.setLayout(null);
			Paneltitle.setBounds(new Rectangle(181, 15, 153, 30));
			Paneltitle.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red));
			Paneltitle.add(lbPostmortem, null);
		}
		return Paneltitle;
	}

	private JPanel getPanelInfo() {
		if (PanelInfo == null) {
			LabelImage = new JLabel();
			LabelImage.setBounds(new Rectangle(30, 192, 74, 26));
			LabelImage.setText("Image:");
			LabelAddress = new JLabel();
			LabelAddress.setBounds(new Rectangle(29, 149, 75, 27));
			LabelAddress.setText("Address:");
			LabelSex = new JLabel();
			LabelSex.setBounds(new Rectangle(30, 105, 75, 28));
			LabelSex.setText("Sex:");
			LabelAge = new JLabel();
			LabelAge.setBounds(new Rectangle(30, 61, 77, 27));
			LabelAge.setText("Age:");
			Labelname = new JLabel();
			Labelname.setBounds(new Rectangle(30, 16, 76, 27));
			Labelname.setText("Name:");
			PanelInfo = new JPanel();
			PanelInfo.setLayout(null);
			PanelInfo.setBounds(new Rectangle(24, 61, 469, 233));
			PanelInfo.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(51, 51, 255), 1), "Infomation", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 255)));
			PanelInfo.add(Labelname, null);
			PanelInfo.add(LabelAge, null);
			PanelInfo.add(LabelSex, null);
			PanelInfo.add(LabelAddress, null);
			PanelInfo.add(LabelImage, null);
			PanelInfo.add(getTxtname(), null);
			PanelInfo.add(getTxtage(), null);
			PanelInfo.add(getTxtaddress(), null);
			PanelInfo.add(getTxtimage(), null);
			PanelInfo.add(getButtonchoose(), null);
			PanelInfo.add(getPanelimage(), null);
			PanelInfo.add(getRadioButtonMale(), null);
			PanelInfo.add(getRadioButtonFemale(), null);
			group = new ButtonGroup();
			group.add(getRadioButtonMale());
			group.add(getRadioButtonFemale());
		}
		return PanelInfo;
	}

	private JTextField getTxtname() {
		if (txtname == null) {
			txtname = new JTextField();
			txtname.setLocation(new Point(120, 18));
			txtname.setSize(new Dimension(164, 26));
		}
		return txtname;
	}

	private JTextField getTxtage() {
		if (txtage == null) {
			txtage = new JTextField();
			txtage.setLocation(new Point(119, 60));
			txtage.setSize(new Dimension(164, 26));
		}
		return txtage;
	}

	private JTextField getTxtaddress() {
		if (txtaddress == null) {
			txtaddress = new JTextField();
			txtaddress.setLocation(new Point(120, 150));
			txtaddress.setSize(new Dimension(164, 26));
		}
		return txtaddress;
	}

	private JTextField getTxtimage() {
		if (txtimage == null) {
			txtimage = new JTextField();
			txtimage.setLocation(new Point(119, 192));
			txtimage.setSize(new Dimension(164, 26));
		}
		return txtimage;
	}

	private JButton getButtonchoose() {
		if (Buttonchoose == null) {
			Buttonchoose = new JButton();
			Buttonchoose.setSize(new Dimension(82, 25));
			Buttonchoose.setText("Browse");
			Buttonchoose.setLocation(new Point(295, 192));
		}
		return Buttonchoose;
	}

	private JPanel getPanelimage() {
		if (Panelimage == null) {
			Labelimage = new JLabel();
			Labelimage.setBounds(new Rectangle(4, 6, 126, 109));
			Labelimage.setText("");
			Panelimage = new JPanel();
			Panelimage.setLayout(null);
			Panelimage.setBounds(new Rectangle(314, 31, 134, 131));
			Panelimage.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(51, 51, 255), 1), "Image", TitledBorder.CENTER, TitledBorder.BOTTOM, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 255)));
			Panelimage.add(Labelimage, null);
		}
		return Panelimage;
	}

	private JPanel getPanelDetail() {
		if (PanelDetail == null) {
			LabelResult = new JLabel();
			LabelResult.setBounds(new Rectangle(30, 91, 79, 25));
			LabelResult.setText("Result:");
			LabelDate = new JLabel();
			LabelDate.setBounds(new Rectangle(300, 58, 70, 24));
			LabelDate.setText("Date:");
			LabelPlace = new JLabel();
			LabelPlace.setBounds(new Rectangle(30, 56, 83, 25));
			LabelPlace.setText("Place:");
			LabelChooseFir = new JLabel();
			LabelChooseFir.setBounds(new Rectangle(299, 18, 75, 24));
			LabelChooseFir.setText("Choose HIS:");
			LabelDoctor = new JLabel();
			LabelDoctor.setText("Doctor Name:");
			LabelDoctor.setLocation(new Point(30, 18));
			LabelDoctor.setSize(new Dimension(87, 25));
			PanelDetail = new JPanel();
			PanelDetail.setLayout(null);
			PanelDetail.setSize(new Dimension(528, 217));
			PanelDetail.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(51, 51, 255), 1), "Detail", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 255)));
			PanelDetail.setLocation(new Point(24, 311));
			PanelDetail.add(LabelDoctor, null);
			PanelDetail.add(getTxtDoctorname(), null);
			PanelDetail.add(LabelChooseFir, null);
			PanelDetail.add(getComboBoxFIR(), null);
			PanelDetail.add(LabelPlace, null);
			PanelDetail.add(getTxtplace(), null);
			PanelDetail.add(LabelDate, null);
			PanelDetail.add(getTxtDate(), null);
			PanelDetail.add(LabelResult, null);
			PanelDetail.add(getTxtResult(), null);
		}
		return PanelDetail;
	}
	
	private JTextField getTxtDoctorname() {
		if (txtDoctorname == null) {
			txtDoctorname = new JTextField();
			txtDoctorname.setLocation(new Point(119, 17));
			txtDoctorname.setSize(new Dimension(164, 26));
		}
		return txtDoctorname;
	}

	private JComboBox getComboBoxFIR() {
		if (ComboBoxFIR == null) {
			ComboBoxFIR = new JComboBox();
			ComboBoxFIR.setBounds(new Rectangle(385, 18, 123, 26));
			ArrayList<HistoryModel> his = HistoryDao.getALL();
			for(HistoryModel model : his)
			{
				ComboBoxFIR.addItem(model.getName());
			}
		}
		return ComboBoxFIR;
	}

	private JTextField getTxtplace() {
		if (txtplace == null) {
			txtplace = new JTextField();
			txtplace.setLocation(new Point(119, 57));
			txtplace.setSize(new Dimension(164, 26));
		}
		return txtplace;
	}

	private JDateChooser getTxtDate() {
		if (txtDate == null) {
			txtDate = new JDateChooser();
			txtDate.setDateFormatString("MM/dd/yyyy");
			txtDate.setLocation(new Point(384, 57));
			txtDate.setSize(new Dimension(124, 26));
			txtDate.getDateEditor().setEnabled(false);
		}
		return txtDate;
	}

	private JTextArea getTxtResult() {
		if (txtResult == null) {
			txtResult = new JTextArea();
			txtResult.setBounds(new Rectangle(119, 94, 387, 111));
		}
		return txtResult;
	}

	private JButton getBtnSave() {
		if (btnSave == null) {
			btnSave = new JButton();
			btnSave.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnSave.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
			btnSave.setText("Save");
			btnSave.setSize(new Dimension(92, 28));
			btnSave.setLocation(new Point(350, 540));
			btnSave.setHorizontalAlignment(SwingConstants.LEFT);
			btnSave.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// validate
				
				    	 PostMotemModel model = new PostMotemModel();
							if(panelPostmortem.idpost>=0)
							{
								model.setId(panelPostmortem.idpost);
							}
							model.setName(txtname.getText());
							model.setAge(Integer.parseInt(txtage.getText()));
							if(RadioButtonMale.isSelected())
							{
								model.setSex(1);
							}
							else
							{
								model.setSex(0);
							}
							SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
							String da = sdf.format(txtDate.getDate());
							model.setDate(da);
							model.setAddress(txtaddress.getText());
							model.setImage(txtimage.getText());
							model.setDoctor(txtDoctorname.getText());
							model.setPlace(txtplace.getText());
							model.setIdHis(FIRDAO.getid("history", ComboBoxFIR.getSelectedItem().toString()));
							//JOptionPane.showMessageDialog(null, FIRDAO.getid("history", ComboBoxFIR.getSelectedItem().toString()));
							java.util.Date now = new java.util.Date();
							String d = sdf.format(now);
							model.setLastUpdated(d);
							model.setResult(txtResult.getText());
							// process 
							if(panelPostmortem.idpost>=0)
							{
								int rs = PostMotemDao.updatePostmotem(model);
								if(rs==1)
								{
									JOptionPane.showMessageDialog(null, "Update sucessful!");
									loadData.loadDataTablePostmortem();
									if(login.idrole==0)
										loadData.tab=4;
									else
										loadData.tab=3;
								}
								else
								{
									JOptionPane.showMessageDialog(null, "Update failed!");
								}
							}
							else
								{
									int rs = PostMotemDao.insertPostmotem(model);
									if(rs==1)
									{
										JOptionPane.showMessageDialog(null, "Insert sucessful!");
										loadData.loadDataTablePostmortem();
										if(login.idrole==0)
											loadData.tab=4;
										else
											loadData.tab=3;
									}
									else
									{
										JOptionPane.showMessageDialog(null, "Insert failed!");
									}
								}
							mainForm a = new mainForm();
							a.setVisible(true);	
							dispose();
				     }
					
				
			});
		}
		return btnSave;
	}

	private JButton getBtnClose() {
		if (btnClose == null) {
			btnClose = new JButton();
			btnClose.setBounds(new Rectangle(458, 540, 94, 28));
			btnClose.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnClose.setIcon(new ImageIcon(getClass().getResource("/image/Close.png")));
			btnClose.setText("Close");
			btnClose.setHorizontalAlignment(SwingConstants.LEFT);
			btnClose.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if(login.idrole==0)
						loadData.tab=4;
					else
						loadData.tab=3;
					mainForm a = new mainForm();
					a.setVisible(true);	
					dispose();
					
				}
			});
		}
		return btnClose;
	}
	
	private void loadDataByID()
	{
		if(panelPostmortem.idpost>=0)
		{
		lbPostmortem.setText("Update Postmortem");
		lbPostmortem.setSize(new Dimension(140, 28));
		btnSave.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
		btnSave.setText("Save");
		btnSave.setSize(new Dimension(90, 28));
		ArrayList<PostMotemModel> post = PostMotemDao.getbyID(panelPostmortem.idpost);
		for(PostMotemModel model : post)
		{
			txtname.setText(model.getName());
			txtage.setText(String.valueOf(model.getAge()));
			if(model.getSex()==1)
			{
				RadioButtonMale.setSelected(true);
			}
			else
			{
				RadioButtonFemale.setSelected(true);
			}
			txtaddress.setText(model.getAddress());
			//image
			txtimage.setText(model.getImage());
			txtDoctorname.setText(model.getDoctor());
			txtplace.setText(model.getPlace());
			try{
				SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
				txtDate.setDate(sdf.parse(model.getDate()));
				}catch (Exception e) {
					e.printStackTrace();
				}
			txtResult.setText(model.getResult());
		
		}
	}
	}

	private JRadioButton getRadioButtonMale() {
		if (RadioButtonMale == null) {
			RadioButtonMale = new JRadioButton();
			RadioButtonMale.setBounds(new Rectangle(120, 106, 76, 25));
			RadioButtonMale.setText("Male");
		}
		return RadioButtonMale;
	}

	private JRadioButton getRadioButtonFemale() {
		if (RadioButtonFemale == null) {
			RadioButtonFemale = new JRadioButton();
			RadioButtonFemale.setBounds(new Rectangle(195, 105, 85, 27));
			RadioButtonFemale.setText("Female");
		}
		return RadioButtonFemale;
	}


}
